package com.gaoyi.basemodule.basevm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import java.util.ArrayList;
import java.util.List;

public class BaseListViewModel<D> extends BaseViewModel {

    protected MutableLiveData<List<D>> mList;
    protected MutableLiveData<String> mKeyWord;

    public BaseListViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<List<D>> getList() {
        if (mList == null) {
            mList = new MutableLiveData<>();
            mList.setValue(new ArrayList<>());
        }
        return mList;
    }

    public MutableLiveData<String> getKeyWord() {
        if (mKeyWord == null) {
            mKeyWord = new MutableLiveData<>();
        }
        return mKeyWord;
    }

    public void setListValue(List<D> data) {
        mList.setValue(data);
    }

    public void addListValue(List<D> data) {
        List<D> newList = new ArrayList<>(mList.getValue());
        newList.addAll(data);
        mList.setValue(newList);
    }

    public void addListValue(D data){
        List<D> newList = new ArrayList<>(mList.getValue());
        newList.add(data);
        mList.setValue(newList);
    }

    public void removeListValue(int position) {
        List<D> newList = new ArrayList<>(mList.getValue());
        newList.remove(position);
        mList.setValue(newList);
    }

    public void clearListValue() {
        mList.setValue(new ArrayList<>());
    }

}
